package com.kaishengit.dao;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.Contact;
import com.kaishengit.util.DBhelp;
import com.kaishengit.util.RowMapper;

public class ContactDao {
	DBhelp<Contact> help = new DBhelp<Contact>();
	
	public void insert(Contact c){
		String sql = "insert into contact(c_id,c_name,c_company,c_mobile,c_tel,c_email,c_address,c_website,c_weibo,c_introduct,pid)values(?,?,?,?,?,?,?,?,?,?,?)";
		help.executeSQL(sql, c.getC_id(),c.getC_name(),c.getC_company(),c.getC_mobile(),c.getC_tel(),c.getC_email(),c.getC_address(),c.getC_website(),c.getC_weibo(),c.getC_introduct(),c.getPid());
	}

	public List<Contact> findAll(String pid) {
		String sql = "select c_id,c_name,c_company,c_mobile,c_tel,c_email,c_address,c_website,c_weibo,c_introduct,pid from contact where pid = ?";
		return help.executeForList(sql, new ContactRowMapper(), pid);
	}
	private class ContactRowMapper implements RowMapper<Contact>{

		public Contact mapper(ResultSet rs) throws SQLException {
			Contact c = new Contact();
			c.setC_id(rs.getString("c_id"));
			c.setC_name(rs.getString("c_name"));
			c.setC_company(rs.getString("c_company"));
			c.setC_mobile(rs.getString("c_mobile"));
			c.setC_tel(rs.getString("c_tel"));
			c.setC_email(rs.getString("c_email"));
			c.setC_website(rs.getString("c_website"));
			c.setC_introduct(rs.getString("c_introduct"));
			c.setC_weibo(rs.getString("c_weibo"));
			c.setPid(rs.getString("pid"));
			return c;
		}
		
		
	}
	public Contact findById(String cid) {
		String sql = "select c_id,c_name,c_company,c_mobile,c_tel,c_email,c_address,c_website,c_weibo,c_introduct,pid from contact where c_id = ?";
		return help.executeQuery(sql, new ContactRowMapper(), cid);
	}

}
